# -*- coding: utf-8 -*-
# @Time    : 2019/10/21 13:26
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : gen_func.py

# 生成器函数，函数中只要有yield关键字


def gen_func():
    yield 1
    yield 2
    yield 3


def fib(index):
    if index <= 2:
        return 1
    else:
        return fib(index - 1) + fib(index - 2)


def fib2(index):
    res_list = []
    n, a, b = 0, 0, 1
    while n < index:
        res_list.append(b)
        a, b = b, a + b
        n += 1
    return res_list

print(fib2(10))


def gen_fib2(index):
    n, a, b = 0, 0, 1
    while n < index:
        yield b
        a, b = b, a + b
        n += 1


for data in gen_fib2(10):
    print(data)

# 惰性求值， 延迟求值提供可能


def func():
    return 1


if __name__ == '__main__':
    # 生成器对象，python编译字节码的时候就产生了
    gen = gen_func()
    for value in gen:
        print(value)
    res = func()
    pass
